using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ToDoListWeb.Models;
using ToDoListWeb.ViewModels;

namespace ToDoListWeb.Service
{
    public class UserService
    {
        private ToDoListWebContext _ctx = new ToDoListWebContext();

        /// <summary>
        /// 用户注册；
        /// 若用户名已存在，抛出异常；
        /// 若用户名不存在，新建Users对象，添加到_ctx下Users列表中；
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="userPassword"></param>
        public bool Register(string userName, string userPassword, string nickName)                          //用户注册                                                                          //用户注册：使用三个参数：用户昵称、用户密码、用户名。
        {
            if (_ctx.Users.SingleOrDefault(p => p.UserName == userName) == null)
            {
                var user = new User()
                {
                    UserName = userName,
                    Password = userPassword,
                    NickName = nickName,
                    RegistTime = DateTime.Now
                };
                _ctx.Users.Add(user);
                _ctx.SaveChanges();
                return true;
            }
            else
                return false;
        }
        /// <summary>
        /// 用户登录；
        /// 若所给数据在_ctx下的Users列表中无匹配项，抛出异常；
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="userPassword"></param>
        /// <returns></returns>
        public bool Login(string userName, string password)                           //用户登录：用户使用用户名以及密码登录。
        {
            return _ctx.Users.Any(user => user.UserName == userName && user.Password == password);
        }

        /// <summary>
        /// 通过userId获取一个Users对象；
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public User GetUser(int userId)
        {
            return _ctx.Users.Include("Inventory").Include("Type").Include("Matter").SingleOrDefault(p => p.UserId == userId);
        }
        public User GetUser(string userName, string password)
        {
            return _ctx.Users.Include("Inventory").Include("Type").Include("Matter").SingleOrDefault(p => p.UserName == userName && p.Password == password);
        }
        /// <summary>
        /// 以UserModel的形式返回部分User对象
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public UserModel GetUserModel(int userId)
        {
            User user = GetUser(userId);
            UserModel userModel = new UserModel()
            {
                UserId = userId,
                UserName = user.UserName,
                NickName = user.NickName,
                RegistTime = user.RegistTime,
                Type = user.Type.ToList(),
                Inventory = user.Inventory.ToList(),
                Matter = user.Matter.ToList()
            };
            return userModel;
        }
        /// <summary>
        /// 修改用户昵称
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="nickName"></param>
        public void ChangeNickName(int userId, string nickName)
        {
            _ctx.Users.Single(u => u.UserId == userId).NickName = nickName;
            _ctx.SaveChanges();
        }
        /// <summary>
        /// 从数据库更新UserModel内容
        /// </summary>
        /// <param name="userModel"></param>
        public void UpdateUserModel(UserModel userModel)
        {
            //var user = _ctx.Users.Single(u => u.UserId == userModel.UserId);
            var user = _ctx.Users.SingleOrDefault(usr => usr.UserId == userModel.UserId);
            userModel.NickName = user.NickName;
            userModel.Type = user.Type;
            userModel.Inventory = user.Inventory;
            userModel.Matter = user.Matter;
            IsPastdue(userModel.UserId);
        }

        public MenuInfoModel GetMenuInfoModel(UserModel user)
        {
            MenuInfoModel menuModel = new MenuInfoModel()
            {
                User = user,
                NumberOfMatter = user.Matter.Count,
                NumberOfPastdueMatter = GetPastdueMatters(user.UserId).Count
            };
            return menuModel;
        }
        /// <summary>
        /// 对该userId对应所有Matter进行判断；
        /// 如果对应事件未设置过期时间，事件未过期，IsPastdue设置为false；
        /// 如果对应事件已过期，IsPastdue设置为true;
        /// 如果对应时间未过期，IsPastdue设置为false;
        /// </summary>
        /// <param name="userId"></param>
        public bool IsPastdue(int userId)                                                   //计算事件是否过期
        {
            var matter = _ctx.Matter.Where(matters => matters.UserId == userId).ToList();
            for (int i = 0; i < matter.Count; i++)
            {
                if (matter[i].OverTime != null && matter[i].OverTime < DateTime.Now)
                {
                    matter[i].IsPastdue = true;
                    return true;
                }
                else
                {
                    matter[i].IsPastdue = false;
                    break;
                }
            }

            return false;
        }
        /// <summary>
        /// 先对userId中所有Matter进行判断；
        /// 返回Matter中所有IsPastdue值为true的事件；
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<Matter> GetPastdueMatters(int userId)                                        //统计过期事件
        {
            IsPastdue(userId);
            List<Matter> loseValueMatter = _ctx.Matter.Where(props => props.UserId == userId && props.IsPastdue == true).ToList();
            return loseValueMatter;
        }
    }
}
